package com.lanou.lenanapp.activity;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;


import com.lanou.lenanapp.R;
import com.lanou.lenanapp.db.DatabaseHelper;
import com.lanou.lenanapp.entity.greendao.LoginEntity;

import java.util.List;

/**
 * 找回密码
 */
public class RetrieveActivity extends Activity implements View.OnClickListener {

    private Button startretrieve;//找回按钮
    private ImageView back_retrieve;
    private EditText editPhone;//输入的手机号
    private DatabaseHelper helper;
    private boolean is;//查询数据库是否存在输入的手机号
    private List<LoginEntity> list;
    private String psw;//按手机号查询密码
    private NotificationManager manager;//定义一个消息管理对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_retrieve);
        helper = DatabaseHelper.getHelper();
        startretrieve = (Button) findViewById(R.id.startretrieve);
        startretrieve.setOnClickListener(this);
        editPhone = (EditText) findViewById(R.id.editPhone);
        //通过系统方法获得notificationmanager对象
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        back_retrieve = (ImageView) findViewById(R.id.back_retrieve);
        back_retrieve.setOnClickListener(this);
    }


    /**
     * 点击找回密码
     * **/
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_retrieve:
                finish();
                break;
            case R.id.startretrieve:
                String phone = editPhone.getText().toString();//获得输入的手机号
                is = helper.isLoginSave(phone);//查询数据库是否存在这个手机号
                if (is){
                    list = helper.queryByIdLogin(phone);//按手机号查询这一条的信息
                    psw = list.get(0).getPsw();
                    //构建一个notification对象
                    Notification.Builder builder = new Notification.Builder(this);
                    builder.setShowWhen(true);
                    builder.setSmallIcon(R.mipmap.ic_launcher);
                    builder.setContentText("您的密码为 : " + psw);
                    builder.setContentTitle("段子新闻大集锦");
                    builder.setWhen(System.currentTimeMillis());//取系统时间
                    manager.notify(100, builder.getNotification());
                }else {
                    Toast.makeText(this, "该手机号还没有注册", Toast.LENGTH_SHORT).show();
                }
                break;
        }


    }


}

